<template>
  <div>
    <FilterTable
      ref="table"
      columnsRef="fyhRepairStuff"
      :isSelectColumn="false"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :dataSource="list"
      height="auto"
      :datakey="'rows'"
      :onColumnsChange="columns => (this.columns = columns)"
    ></FilterTable>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { all } from 'q';
import { queryUnitCode } from '@/api/part/partInfo';
import { getChexSelectTable } from '@/api/repair/repairReception/customer/brand.js';
import { queryRepairLabour, queryRepairPartYg, querySellMaterial, queryChargeDifferentiationList } from '@/api/repair/repairReception/repairBusinessManger';
import { getSeries } from '@/api/customerRepair/customerCare/insuranceDueRemind/index';
import { getSystemDefault } from '@/api/repair/repairReception/customer';
import { getAllUser } from '@/api/customerRepair/activityVisit/resultEntry'; // 查询所有用户
import moment from 'moment';
export default {
  name: 'RepairPro',
  mixins: [dictionary],
  props: ['dataList', 'roNo'],

  data() {
    return {
      columns: this.createTable(),
      list: [],
      fetchApi: queryRepairPartYg,
      fetchParams: {},
      orderStatus: '' // EP判断一车一单
    };
  },
  created() {
    let roNo = this.roNo;
    this.fetchParams = Object.assign({}, { roNo: roNo }, { xhrAbort: false });
    this.getAllUser();
  },
  mounted() {
    // this.queryRepairMaterial(); // 维修材料查询
    this.queryChargeList();
    this.querySeries();
    this.getSystemDefault();
    this.getAllUser();
    this.getUnitCode();
  },
  methods: {
    createTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '收费区分',
          dataIndex: 'CHARGE_PARTITION_CODE',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editable: false,
          editItems: [],
          editType: 'select'
        },

        {
          title: '仓库代码',
          dataIndex: 'STORAGE_CODE',
          sorter: true,
          filterType: 'input',
          filter: true,
          render: props => {
            return <span>{props.row.STORAGE_CODE}</span>;
          }
        },
        {
          title: '备件代码',
          dataIndex: 'PART_NO',
          sorter: true,
          filterType: 'input',
          filter: true,
          showOverflowTooltip: true,
          render: props => {
            return <span>{props.row.PART_NO}</span>;
          }
        },
        {
          title: '备件名称',
          dataIndex: 'PART_NAME',
          filterType: 'input',
          sorter: true,
          filter: true,
          editable: false,
          editType: 'text',
          showOverflowTooltip: true,
          render: props => {
            return <span>{props.row.PART_NAME}</span>;
          }
        },
        {
          title: '备件别名',
          dataIndex: 'PART_NAME1',
          filterType: 'input',
          sorter: true,
          filter: true,
          editable: false,
          editType: 'text',
          hidden: true,
          render: props => {
            return <span>{props.row.PART_NAME1}</span>;
          }
        },
        {
          title: '主要备件',
          dataIndex: 'IS_MAIN_PART',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          editable: false,
          editType: 'select',
          editItems: this.createDictList('1004')
        },
        {
          title: '备件数量',
          dataIndex: 'PART_QUANTITY',
          filterType: 'number',
          sorter: true,
          filter: true,
          summation: true
        },
        {
          title: '计量单位',
          dataIndex: 'UNIT_CODE',
          sorter: true,
          filterType: 'checkbox',
          filterItems: [],
          editable: false,
          editType: 'select',
          editItems: []
          // filter: true
          // render: props => {
          //   return <span>{props.row.UNIT_CODE}</span>;
          // }
        },
        {
          title: '备件销售单价',
          dataIndex: 'PART_SALES_PRICE',
          filterType: 'number',
          sorter: true,
          filter: true,
          initialValue: '0',
          render: props => {
            return <span>{props.row.PART_SALES_PRICE}</span>;
          }
        },
        {
          title: '备件销售金额',
          dataIndex: 'PART_SALES_AMOUNT',
          filterType: 'number',
          sorter: true,
          filter: true,
          render: props => {
            return <span>{props.row.PART_SALES_AMOUNT}</span>;
          },
          precision: 2,
          summation: true
        },
        {
          title: '备件成本单价',
          dataIndex: 'PART_COST_PRICE',
          filterType: 'number',
          summationPrecision: 2,
          sorter: true,
          filter: true
        },
        {
          title: '备件成本金额',
          dataIndex: 'PART_COST_AMOUNT',
          filterType: 'number',
          sorter: true,
          filter: true
        },
        {
          title: '优惠金额',
          dataIndex: 'DISCOUNT_AMOUNT',
          filterType: 'number',
          sorter: true,
          filter: true,
          precision: 2,
          summation: true
        },
        {
          title: '折扣率',
          dataIndex: 'DISCOUNT',
          filterType: 'number',
          sorter: true,
          filter: true,
          editable: false,
          editType: 'text'
        },
        {
          title: '实收金额',
          dataIndex: 'RECEIVE_AMOUNT',
          filterType: 'number',
          sorter: true,
          filter: true,
          precision: 2,
          summation: true
        },
        {
          title: '是否入账',
          dataIndex: 'IS_FINISHED',
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          sorter: true,
          filter: true,
          render: props => {
            return <span>{this.createDictText(props.row.IS_FINISHED, '1004')}</span>;
          }
        },
        {
          title: '发料人',
          dataIndex: 'SENDER',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editable: false,
          editType: 'select',
          editItems: []
        },
        {
          title: '不可打折',
          dataIndex: 'IS_DISCOUNT',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          disabled: true,
          render: props => {
            return <span>{this.createDictText(props.row.IS_DISCOUNT, '1004')}</span>;
          }
        },
        {
          title: '是否增项',
          dataIndex: 'IS_ADDITION',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            // return <span>{this.createDictText(props.row.IS_ADDITION, '1004')}</span>;
            return <el-checkbox checked={props.row.IS_ADDITION == 10041001} disabled></el-checkbox>;
          }
        },
        {
          title: '领料人',
          dataIndex: 'RECEIVER',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editable: false,
          editType: 'select',
          editItems: []
        },
        {
          title: '是否不修',
          dataIndex: 'NEEDLESS_REPAIR',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          editable: false,
          render: props => {
            return <span>{this.createDictText(props.row.NEEDLESS_REPAIR, '1004')}</span>;
          }
        },
        {
          title: '是否预检',
          dataIndex: 'PRE_CHECK',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          editable: false,
          render: props => {
            return <span>{this.createDictText(props.row.PRE_CHECK, '1004')}</span>;
          }
        },
        {
          title: '是否委外',
          dataIndex: 'CONSIGN_EXTERIOR',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          editable: false,
          render: props => {
            return <span>{this.createDictText(props.row.CONSIGN_EXTERIOR, '1004')}</span>;
          }
        },
        {
          title: '流水号',
          dataIndex: 'BATCH_NO',
          sorter: true,
          filterType: 'input',
          filter: true
          // render: props => {
          //   return <span>{props.row.BATCH_NO}</span>;
          // }
        },
        {
          title: '预拣单打印流水号',
          dataIndex: 'PRINT_BATCH_NO',
          sorter: true,
          filterType: 'input',
          filter: true
          // render: props => {
          //   return <span>{props.row.PRINT_BATCH_NO}</span>;
          // }
        },
        {
          title: '组合代码',
          dataIndex: 'PACKAGE_CODE',
          filterType: 'input',
          sorter: true,
          filter: true
          // render: props => {
          //   return <span>{props.row.PACKAGE_CODE}</span>;
          // }
        },
        {
          title: '维修项目代码',
          dataIndex: 'LABOUR_CODEF',
          sorter: true,
          filterType: 'input',
          filter: true
          // render: props => {
          //   return <span>{props.row.PACKAGE_CODE}</span>;
          // }
        },
        {
          title: '车系',
          dataIndex: 'MODEL_LABOUR_CODE',
          sorter: true,
          filter: true,
          editable: false,
          filterType: 'checkbox',
          filterItems: [],
          editType: 'select',
          editItems: []
        },
        {
          title: '活动编号',
          dataIndex: 'ACTIVITY_CODE',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '合同编号',
          dataIndex: 'SSP_CONTRACT_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备件单价差价',
          dataIndex: 'ASC_SHORTNAME',
          filterType: 'number',
          sorter: true,
          filter: true
        },
        {
          title: '备件差价总额',
          dataIndex: 'PART_AMOUNT_GAP',
          filterType: 'number',
          sorter: true,
          filter: true
        },
        {
          title: '备件类型',
          dataIndex: 'PART_CATEGORY',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <span>{this.createDictText(props.row.PART_CATEGORY, '1149')}</span>;
          }
        },
        {
          title: '创建时间',
          dataIndex: 'CREATED_AT',
          sorter: true,
          filterType: 'date-range',
          filter: true,
          dateFormat: 'YYYY-MM-DD HH:mm'
        }
      ];
    },
    // 所有用户
    async getAllUser() {
      const res = await getAllUser();
      // console.log('所有用户', res);
      this.columns.find(item => item.dataIndex === 'SENDER').editItems = this.columns.find(item => item.dataIndex === 'SENDER').filterItems = res.data.map(x => ({
        value: x.userId + '',
        text: x.employeeName
      }));
      this.columns.find(item => item.dataIndex === 'RECEIVER').editItems = this.columns.find(item => item.dataIndex === 'RECEIVER').filterItems = res.data.map(x => ({
        value: x.userId + '',
        text: x.employeeName
      }));
    },
    async getUnitCode() {
      const res3 = await queryUnitCode();
      this.formList.find(item => item.dataIndex == 'UNIT_CODE').editItems = res3.data.map(x => ({ text: x.UNIT_NAME, value: x.UNIT_CODE }));
    },
    async getSystemDefault() {
      this.orderStatus = (await getSystemDefault({ itemCode: 8033 })).data;
      // EP判断一车一单
      if (this.orderStatus == '10041001') {
        this.columns.find(x => x.dataIndex === 'PART_NAME1').hidden = false; // 显示备件别名
      }
    },
    createBaseTable() {
      this.BaseTable = this.$refs.table.$refs.pageTable;
    },
    daList(list) {
      this.list = list;
    },

    // 查询所有车系(供table使用)
    async querySeries() {
      const res = await getChexSelectTable();
      this.columns.find(x => x.dataIndex === 'MODEL_LABOUR_CODE').editItems = res.data.map(x => ({ value: x.seriesId + '', text: x.seriesName }));
    },

    // 查询收费区分
    async queryChargeList() {
      const res = await queryChargeDifferentiationList();
      // console.log(res, 'res');
      this.columns.find(x => x.dataIndex === 'CHARGE_PARTITION_CODE').editItems = this.columns.find(x => x.dataIndex === 'CHARGE_PARTITION_CODE').filterItems = res.data.map(x => ({
        value: x.CHARGE_PARTITION_CODE + '',
        text: x.CHARGE_PARTITION_NAME
      }));
    }

    // 维修材料查询
    // async queryRepairMaterial() {
    //   const roNo = this.roNo;
    //   const res = (await queryRepairPartYg(roNo)).data.rows;
    //   this.list = res;
    // }

    // 传给父组件
    // syncTableFun(val) {
    //   if (val && val.length > 0) {
    //     delete val.dataMark;
    //     this.$emit('syncTablelist', val);
    //   } else {
    //     this.$emit('syncTablelist', []);
    //   }
    // }
  }
};
</script>

<style lang="less"></style>
